package asionreachel.cn.smarthomedemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.base.BaseActivity;
import asionreachel.cn.smarthomedemo.base.Common;


/**
 * Created by Administrator on 2016/11/18.
 * 填写WiFi信息界面
 */

public class SelectWifiActivity extends BaseActivity {
    public static final int s_iRequestCodeRedeploy = 0;

    private EditText edAccount;

    private EditText edPass;

    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
        if( s_iRequestCodeRedeploy == requestCode ) {
            if( RESULT_OK != resultCode ) {
                return;
            }

            setResult( RESULT_OK );
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;

        setContentView( R.layout.activity_fill_wifi );

        ImageView ivBack = (ImageView)findViewById(R.id.iv_back);
        TextView tvConnect = (TextView)findViewById(R.id.fill_tv_connect);
        edAccount = (EditText)findViewById(R.id.fill_ed_account);
        edPass = (EditText)findViewById(R.id.fill_ed_pass);
        Button btnNext = (Button)findViewById(R.id.fill_btn_yes);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });

        tvConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
                startActivity(wifiSettingsIntent);
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = edAccount.getText().toString().trim();
                String pass = edPass.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Common.showToast("请输入Wifi账号");
                    return;
                }if(TextUtils.isEmpty(pass)){
                    Common.showToast("请输入Wifi密码");
                    return;
                }
                if(pass.length()<8){
                    Common.showToast("Wifi密码格式不正确");
                    return;
                }
                Common.saveWifiPassword(account , pass);
                startActivityForResult( new Intent(mActivity,RedeployActivity.class).putExtra("account",account).putExtra("pass",pass), s_iRequestCodeRedeploy );
            }
        });

        CheckBox cbPW = (CheckBox)findViewById(R.id.fillwifi_cb_pw);
        boolean bCheck = cbPW.isChecked();
        Common.setEditTextStyle(edPass , bCheck);
        cbPW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Common.setEditTextStyle(edPass , isChecked);
            }
        });
    }

    public void onBack() {
        finish();
    }

    @Override
    public void onBackKey() {
        onBack();
    }

    @Override
    protected void onResume() {
        super.onResume();

        String wifiName = Common.getWifiName();
        edAccount.setText(wifiName);
        edPass.setText(Common.getWifiPassword(wifiName));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}